﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPEmulatorImageLoader
{
    public partial class MainWindow : Window
    {
        //http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh286408(v=vs.105).aspx
        readonly string toolPath = @"C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v8.0\Tools\IsolatedStorageExplorerTool\ISETool.exe";
        readonly string wpAppId = "d7ba7d9b-3e7e-43ce-ba63-5c857a8208a9";

        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnSelectCameraRollFolder_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                txtCameraRollLocation.Text = dlg.SelectedPath;
        }

        private void btnSelectSavedPicturesFolder_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                txtSavedPicturesLocation.Text = dlg.SelectedPath;
        }

        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            //create a single folder that contains both the camera roll and saved pictures folders together, to be used as a local snapshot
            string localSnapshotPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Snapshot";
            string snapshotCameraRollPath = localSnapshotPath + @"\CameraRoll\";
            string snapshotSavedPicturesPath = localSnapshotPath + @"\SavedPictures\";

            //setup snapshot folders
            setupSnapshotFolder(txtCameraRollLocation.Text, snapshotCameraRollPath, chkCameraRollRecursive.IsChecked.Value);
            setupSnapshotFolder(txtSavedPicturesLocation.Text, snapshotSavedPicturesPath, chkSavedPicturesRecursive.IsChecked.Value);
            
            //copy the ImageLoader app on the emulator (the app must have already been run on the emulator, so it exists)
            ProcessStartInfo info = new ProcessStartInfo();
            info.FileName = toolPath;
            info.Arguments = String.Format(@"rs xd {0} ""{1}""", wpAppId, localSnapshotPath); //rs=restore snapshot, xd=emulator, GUID=app product id
            Process.Start(info);            
        }

        private void setupSnapshotFolder(string sourceFolder, string destinationFolder, bool loadRecursive)
        {
            //make sure the destination folder exists, and is empty
            if (Directory.Exists(destinationFolder))
                Directory.Delete(destinationFolder, true);
            Directory.CreateDirectory(destinationFolder);
            
            //copy all source folders into the destination
            if (Directory.Exists(sourceFolder))
            {
                SearchOption searchOption = loadRecursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;

                foreach (string file in Directory.GetFiles(sourceFolder, "*", searchOption))
                {
                    string name = System.IO.Path.GetFileName(file);
                    string directory = System.IO.Path.GetDirectoryName(file);
                    string subDirectory = directory.Replace(sourceFolder, "");
                    string finalFolder = destinationFolder + subDirectory + '\\';

                    if (!Directory.Exists(finalFolder))
                        Directory.CreateDirectory(finalFolder);

                    if ((name.ToLower().EndsWith(".jpg")) || (name.ToLower().EndsWith(".png")))
                        File.Copy(file, finalFolder + name, true);
                }
            }
        }

        private void btnDisplay_Click(object sender, RoutedEventArgs e)
        {
            ProcessStartInfo info = new ProcessStartInfo();
            info.FileName = "cmd.exe";
            info.Arguments = @" /c """ + toolPath + @""" dir xd " + wpAppId + " & pause"; //emulator
            //info.Arguments = @"dir de " + wpAppId + " & pause"; //device
            info.UseShellExecute = false;
            Process.Start(info);
        }
    }
}
